<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <script>
        // 1.0 数字的累加
        // var arr = [1, 2, 3, 4]
        // var res = arr.reduce(function (pre, cur, idx) {
        //     return pre + cur;
        // }, 0)

        // // 2.0 数组的累加
        // var arr = [1, 2, 3, 4]
        // var res = arr.reduce(function (pre, cur, idx) {
        //     pre.push(cur + 10);
        //     return pre
        // }, [])

        // // 3.0 对象的累加 ?
        // var arr = [1, 2, 3, 4, 5]
        // var res = arr.reduce(function (pre, cur, idx) {
        //     pre[cur] = 1
        //     return pre
        // }, {})

        // 4.0 数组的去重
        var arr = [1, 2, 22, 3, 1, 2, 4, undefined]
        var res = arr.reduce(function (pre, cur, idx) {
            pre.indexOf(cur) === -1 ? pre.push(cur) : null;
            return pre
        }, [])
        console.log(res);

        // 5.0 字符串的去重
        var str = 'acccaceecrr'
        var arr = []
        for(var i = 0 ; i < str.length; i++){
            arr.push(str[i])
        }

        var res = arr.reduce(function(pre,cur,idx){
            typeof pre[cur] === "undefined" ? pre[cur] = 1 : pre[cur]++ ;
            return pre;
        } , {})
        console.log(res);

    </script>
</body>

</html>